package com.d2.framework.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 日志切面类
 * @author mayihua
 */
@Aspect
@Component
@Order(5)
@Slf4j
public class WebLogAspect {

    private final ThreadLocal<Long> startTime=new ThreadLocal<>();


    @Pointcut("execution(public * com.d2.modules..*.*Controller.*(..))")
    public void webLog(){

    }

    @Before(value = "webLog()")
    public void doBefore(JoinPoint point){
        startTime.set(System.currentTimeMillis());

        log.info("WebLogAspect.doBefore............");
        ServletRequestAttributes attributes=
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        log.info("IP : "+request.getRemoteAddr());
        log.info("URL:" + request.getRequestURL().toString());
        log.info("HTTP_METHOD:" +request.getMethod());
        log.info("CLASS_NAME : " + point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());
        log.info("ARGS : " + Arrays.toString(point.getArgs()));
    }


    @AfterReturning(value = "webLog()",returning = "ret")
    public void doAferReturning(Object ret){
        log.info("WebLogAspect.doAfterReturning.............");
        log.info("Resp: " + ret);
        log.info("Spend Time : " + (System.currentTimeMillis()-startTime.get()));
        startTime.remove();
    }
}
